// main.cpp - HarmonyOS版本
// Qt for HarmonyOS 动画演示集合
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QSurfaceFormat>
#include <QtQml>
#include <QDebug>
#include <QGuiApplication>
#include <QCoreApplication>
#include <QOpenGLContext>
#include <QQuickWindow>
#include <QQuickItem>

// 全局对象实例（静态变量，确保生命周期）
static QQmlApplicationEngine *g_engine = nullptr;

extern "C" int qtmain(int argc, char **argv)
{
    // 防止重复初始化
    if (g_engine != nullptr) {
        qDebug() << "AnimationDemo Qt引擎已初始化，跳过重复初始化";
        return 0;
    }
    
    qDebug() << "AnimationDemo 开始初始化Qt应用...";
    
    // ========================================
    // Step 0: 创建 Qt 应用实例
    // ========================================
    QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
    QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
    
    qputenv("QSG_INFO", "1");
    
    QGuiApplication app(argc, argv);
    QCoreApplication::setApplicationName(QStringLiteral("AnimationDemo"));
    QCoreApplication::setOrganizationName(QStringLiteral("AnimationDemo"));
    QCoreApplication::setOrganizationDomain(QStringLiteral("animationdemo.com"));
    
    // ========================================
    // Step 1: 配置 OpenGL ES 表面格式
    // ========================================
    QSurfaceFormat format;
    format.setAlphaBufferSize(8);
    format.setRedBufferSize(8);
    format.setGreenBufferSize(8);
    format.setBlueBufferSize(8);
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(3, 0);
    format.setProfile(QSurfaceFormat::CoreProfile);
    format.setRenderableType(QSurfaceFormat::OpenGLES);
    QSurfaceFormat::setDefaultFormat(format);
    
    // ========================================
    // Step 2: 创建并配置 QML 引擎
    // ========================================
    g_engine = new QQmlApplicationEngine();
    
    // 添加组件搜索路径
    g_engine->addImportPath(QStringLiteral("qrc:/"));
    g_engine->addImportPath(QStringLiteral("qrc:/qml"));
    
    // 加载 QML 文件
    qDebug() << "AnimationDemo 加载QML文件: qrc:/main.qml";
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(g_engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl) {
            qCritical() << "AnimationDemo QML对象创建失败";
            QCoreApplication::exit(-1);
        } else if (obj) {
            qDebug() << "AnimationDemo QML对象创建成功";
        }
    }, Qt::QueuedConnection);
    g_engine->load(url);
    
    // 最稳妥兜底：若加载失败，展示内置应急界面，避免纯白屏
    if (g_engine->rootObjects().isEmpty()) {
        qWarning() << "AnimationDemo QQmlApplicationEngine failed to load component. Fallback UI will be shown.";
        static const char fallbackQml[] =
            "import QtQuick 2.15\n"
            "Rectangle {\n"
            "  anchors.fill: parent\n"
            "  color: \"#FF0000\"\n"
            "  Text { anchors.centerIn: parent; color: \"white\"; text: \"QML加载失败\"; font.pixelSize: 20 }\n"
            "}\n";
        g_engine->loadData(QByteArray(fallbackQml), QUrl(QStringLiteral("qrc:/fallback.qml")));
        if (g_engine->rootObjects().isEmpty()) {
            qCritical() << "AnimationDemo Fallback UI also failed to load. Exiting.";
            return -1;
        }
    } else {
        qDebug() << "AnimationDemo QML文件加载成功";
    }

    // 进入事件循环
    return app.exec();
}

// 提供一个前向到 qtmain() 的适配，以兼容不同加载策略
extern "C" int main(int argc, char **argv)
{
    return qtmain(argc, argv);
}
